from django.db import models


# Create your models here.


class RouteProtect(models.Model):
    ip_address = models.GenericIPAddressField(null=False, blank=False)
    per_minute = models.IntegerField(default=0)
    per_hour = models.IntegerField(default=0)
    per_day = models.IntegerField(default=0)
    route_url = models.URLField(null=False, blank=False)
    last_updated = models.DateTimeField(auto_now=True)

    class Meta:
        indexes = [
            models.Index(fields=['ip_address'], name='route_protect_ip_address_index'),
            models.Index(fields=['route_url'], name='route_protect_route_url_index')
        ]


class WebUser(models.Model):
    """
    网站用户表
    """
    username = models.CharField(max_length=32, null=False, blank=False, unique=True)
    password = models.CharField(max_length=32, null=False, blank=False)
    uuid = models.BigAutoField(primary_key=True, auto_created=True)
    nickname = models.CharField(max_length=32)
    phone = models.CharField(max_length=11)
    email = models.EmailField()
    register_time = models.DateTimeField(auto_now=True)
    available = models.BooleanField(default=True)
    last_login = models.DateTimeField(auto_now=True)

    class Meta:
        indexes = [
            models.Index(fields=['username'], name='user_username_index')
        ]


class RolePermission(models.Model):
    """
    角色权限
    """
    role = models.CharField(max_length=32, null=False, blank=False)
    permission = models.CharField(max_length=32, null=False, blank=False)

    class Meta:
        indexes = [
            models.Index(fields=['role'], name='role_permission_role_index')
        ]


class GroupPermission(models.Model):
    """
    组权限
    """
    group = models.CharField(max_length=32, null=False, blank=False)
    permission = models.CharField(max_length=32, null=False, blank=False)

    class Meta:
        indexes = [
            models.Index(fields=['group'], name='group_permission_group_index')
        ]


class UserRole(models.Model):
    """
    用户角色
    """
    username = models.CharField(max_length=32, null=False, blank=False, unique=True)
    role = models.CharField(max_length=32, null=False, blank=False)
    last_updated = models.DateTimeField(auto_now=True)

    class Meta:
        indexes = [models.Index(fields=['username'], name='user_role_username_index')]


class UserGroup(models.Model):
    """
    用户组
    """
    username = models.CharField(max_length=32, null=False, blank=False, unique=True)
    group = models.CharField(max_length=32, null=False, blank=False)
    last_updated = models.DateTimeField(auto_now=True)

    class Meta:
        indexes = [models.Index(fields=['username'], name='user_group_username_index')]
